//
//  ViewController.m
//  seachConstrollerTest
//
//  Created by MB__Lee on 2018/4/14.
//  Copyright © 2018年 MB__Lee. All rights reserved.
//

#import "ViewController.h"

#import "MBSearchResultVC.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource,UISearchControllerDelegate,UISearchBarDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableview;
@property(nonatomic,strong)NSMutableArray *mut;


@property(nonatomic,strong)MBSearchResultVC *mbvc;



@end

@implementation ViewController

-(NSMutableArray *)mut{
    if (!_mut) {
        _mut  = [NSMutableArray array];
        for (int i = 0; i< 100; i++) {
            [_mut addObject:[self getSting]];
        }
    }
    return  _mut;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    [self searchVC];
    
    NSLog(@"%@",self.mut);
}

-(void)searchVC{
    
     _mbvc = [[MBSearchResultVC alloc] init];
   
    
    UISearchController *searchVC = [[UISearchController alloc] initWithSearchResultsController:_mbvc];
    searchVC.delegate = self;
    searchVC.searchResultsUpdater = _mbvc;
    
    searchVC.dimsBackgroundDuringPresentation = YES;
    searchVC.obscuresBackgroundDuringPresentation = YES;
    
    searchVC.searchBar.delegate = self;
    
    searchVC.searchBar.placeholder = @"搜索";
    
    
    
    
    if (@available(iOS 11.0, *)) {
        self.navigationItem.searchController = searchVC;
    }else{
         self.tableview.tableHeaderView = searchVC.searchBar;
    }
    
    
    _mbvc.arrayFromVC = self.mut;

    
}

#pragma mark --searchBarDelegate
-(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
    return  YES;
}
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
    NSLog(@"-----%@",self.mut);
}
    


#pragma mark ---uisearchControllerDelegat
-(void)presentSearchController:(UISearchController *)searchController{
    
}
-(void)willPresentSearchController:(UISearchController *)searchController{
    
}
-(void)didPresentSearchController:(UISearchController *)searchController{
    
}

-(void)willDismissSearchController:(UISearchController *)searchController{
    
}
-(void)didDismissSearchController:(UISearchController *)searchController{
    
}





#pragma mark ---tableviewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return  1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  self.mut.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identi = @"cell";
    UITableViewCell *cell  = [tableView dequeueReusableCellWithIdentifier:identi];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identi];
        
    }
    
    cell.textLabel.text = self.mut[indexPath.row];
    
    
    return  cell;
}


-(NSString *)getSting{
    NSMutableArray * shuffledAlphabet = [NSMutableArray arrayWithArray:@[@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z"]];
    
    NSString *strTest = [[NSString alloc]init];
    for (int i=0; i<3; i++) {
        int x = arc4random() % 25;
        strTest = [NSString stringWithFormat:@"%@%@",strTest,shuffledAlphabet[x]];
    }
    return strTest;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
